<template>
  <div>
    <!--顶部区域-->
    <!--面包屑导航-->
    <Breadcrumb></Breadcrumb>
    <div class="header">
      <div class="form">
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="预定编号">
            <el-input
              v-model="formInline.name"
              placeholder="预定编号"
              size="small"
            ></el-input>
          </el-form-item>
          <el-form-item label="预定时间">
            <el-date-picker
              v-model="formInline.date"
              type="date"
              placeholder="选择日期"
              size="small"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="small">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="group">
        <el-button type="warning" size="small" @click="orderCollect"
          >订单汇总</el-button
        >
        <!--导出选中的数据-->
        <download-excel
          class="export-excel-wrapper"
          :data="DetailsForm"
          :fields="json_fields"
          :header="title1"
          name="选中记录.xls"
        >
          <el-button type="warning" size="small" 
            >导出选中</el-button
          >
        </download-excel>
        <!--导出所有的数据-->
        <download-excel
          class="export-excel-wrapper"
          :data="tableData"
          :fields="json_fields"
          :header="title"
          name="所有记录.xls"
        >
          <el-button type="warning" size="small">导出所有</el-button>
        </download-excel>
      </div>
    </div>
    <!--表格-->
    <div class="content">
      <el-table
        :data="tableData"
        border
        stripe
        style="width: 100%"
        header-row-class-name="active-header"
        cell-class-name="table-center"
        @select="select"
      >
        <el-table-column
          type="selection"
          width="55"
          header-align="center"
          :selectable="selectable"
        >
        </el-table-column>
        <el-table-column prop="code" label="订单编号" header-align="center">
          <template slot-scope="scope">
            <span
              @click="OpenOrderDecs(scope.row)"
              style="cursor: pointer; color: blue"
              >{{ scope.row.code }}</span
            >
          </template>
        </el-table-column>
        <el-table-column prop="ordername" label="下单人" header-align="center">
        </el-table-column>
        <el-table-column prop="company" label="所属单位" header-align="center">
        </el-table-column>
        <el-table-column prop="phone" label="联系电话" header-align="center">
        </el-table-column>
        <el-table-column
          prop="yudingTime"
          label="预定时间"
          header-align="center"
        >
          <template slot-scope="scope">
            {{ dayjs(scope.row.yudingTime).format("YYYY-MM-DD HH:mm:ss") }}
          </template>
        </el-table-column>
        <el-table-column prop="price" label="订单总价" header-align="center">
        </el-table-column>
        <el-table-column
          prop="huizongStatus"
          label="汇总状态"
          header-align="center"
        >
          <template slot-scope="scope">
            {{ scope.row.huizongStatus == 1 ? "未汇总" : "已汇总" }}
          </template>
        </el-table-column>
      </el-table>
      <Pagination
        :total="total"
        :pageSize="pageSize"
        @CurrentChange="CurrentChange"
      ></Pagination>
      <OrderDesc ref="child"></OrderDesc>
    </div>
  </div>
</template>

<script>
import dayjs from "dayjs";
import Pagination from "@/components/pagination/Pagination.vue";
import OrderDesc from "./OrderDesc.vue";
export default {
  components: {
    Pagination,
    OrderDesc,
  },
  data() {
    return {
      formInline: {},
      tableData: [],
      total: 10,
      pageSize: 1,
      ids: [], //存放当前操作的数组集合
      title1: "选中记录",
      DetailsForm: [], //选中的数据 [{},{}]
      json_fields: {
        //映射字段值
        //"订单编号": 'code',
        //数字处理--
        订单编号: {
          field: "code",
          callback: (value) => {
            return "&nbsp;" + value;
          },
        },
        下单人: "ordername",
        所属单位: "company",
        联系电话: {
          field: "phone",
          callback: (value) => {
            return "&nbsp;" + value;
          },
        },
        预定时间: "yudingTime",
        订单总价格: "price",
        汇总状态: "huizongStatus",
      },
      title: "xxx公司采购系统订单列表", //
    };
  },

  created() {
    this.orderList(1);
  },
  methods: {
    dayjs,
    //订单详情
    OpenOrderDecs() {
      this.$refs.child.drawer = true;
      this.$refs.child.direction = "rtl";
    },
    //订单汇总功能
    orderCollect() {
      if (this.ids.length > 1) {
        this.$api.changeStatus({ ids: this.ids.join(",") }).then((res) => {
          console.log("汇总返回", res.data);
          if (res.data.status == 200) {
            this.orderList(1);
          }
        });
      } else {
        this.$alert("汇总订单至少需要两条订单", "汇总订单", {
          confirmButtonText: "确定",
          callback: (action) => {
            this.$message({
              type: "info",
              message: `取消汇总`,
            });
          },
        });
      }
    },
    //拿到勾选的内容
    select(selection, row) {
      console.log("selection:", selection);
      console.log("row:", row);
      let arr = [];
      selection.forEach((element) => {
        arr.push(element.id);
      });
      this.ids = arr;
      //存储当前勾选行的数据
      this.DetailsForm = selection;
    },

    //获取分页的页码
    CurrentChange(page) {
      this.orderList(page);
    },
    dayjs,
    //禁用复选框
    selectable(row, index) {
      return row.huizongStatus == "未汇总";
    },

    //获取订单列表
    async orderList(page) {
      let res = await this.$api.orderList({ page });
      console.log("订单列表接口----", res.data);
      if (res.status == 200) {
        this.tableData = res.data.data;
        let arr = res.data.data;
        arr.forEach((element) => {
          element.huizongStatus =
            element.huizongStatus == 1 ? "未汇总" : "已汇总";
          element.yudingTime = dayjs(element.yudingTime).format(
            "YYYY-MM-DD HH:mm:ss"
          );
        });
        this.total = res.data.total;
        this.pageSize = res.data.pageSize;
      } else {
        this.tableData = [];
        this.total = 1;
        this.pageSize = 1;
      }
    },
  },
};
</script>

<style lang="less" scoped>
.header {
  background: #fff;
  margin-bottom: 20px;
  padding: 10px;
  .el-form-item {
    margin-bottom: 10px;
  }
  .group {
    border: 1px solid #eee;
    padding: 5px;
  }
}
.content {
  background: #fff;
  /deep/ .active-header {
    text-align: center;
    color: #333;
  }

  /deep/ .table-center {
    text-align: center;
  }
}

.export-excel-wrapper {
  display: inline;
  margin-left: 10px;
  button {
    padding-left: 30px;
    padding-right: 30px;
  }
}
</style>